/*
* File :
* Created : 17 janv. 07
* By : eSpider Team
* Version : $Revision$
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details ( see the LICENSE file ).
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/

package org.fbmc.file;

import java.io.File;

public abstract class MediaFile implements Comparable<MediaFile>{
	public static enum Type {video, music, image};
	
	private static final String[] videoExtensions = {".avi", ".divx"};
	private static final String[] musicExtensions = {".mp3"};
	private static final String[] imageExtensions ={".jpeg", ".jpg"};
	
	private long id;	
	private String path;
	private String name;
	private long size;
	private long lastModified;
	
	public static boolean isMediaFile(File file){
		return isVideoFile(file) || isMusicFile(file) || isImageFile(file);
	}
	
	private static boolean isMusicFile(File file){
		for(String extension : musicExtensions){
			if(file.getName().endsWith(extension))
				return true;
		}
		return false;
	}
	
	private static boolean isVideoFile(File file){
		for(String extension : videoExtensions){
			if(file.getName().endsWith(extension))
				return true;
		}
		return false;
	}
	
	private static boolean isImageFile(File file){
		for(String extension : imageExtensions){
			if(file.getName().endsWith(extension))
				return true;
		}
		return false;
	}
	
	public static MediaFile create(File file){
		if(isImageFile(file))
			return new ImageFile(file);
		else if(isMusicFile(file))
			return new MusicFile(file);
		else if(isVideoFile(file))
			return new VideoFile(file);
		
		return null;
	}
	
	public MediaFile(){}
	
	public MediaFile(File file){
		this.path = file.getAbsolutePath();
		this.name = file.getName();
		this.size = file.length();
		this.lastModified = file.lastModified();
	}
	
	public long getId() {
		return id;
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getLastModified() {
		return lastModified;
	}

	public void setLastModified(long lastModified) {
		this.lastModified = lastModified;
	}

	public String getPath() {
		return path;
	}

	public void setPath(String path) {
		this.path = path;
	}

	public long getSize() {
		return size;
	}

	public void setSize(long size) {
		this.size = size;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}
	
	public int compareTo(MediaFile mediaFile) {
		if(this.size != mediaFile.getSize()) return -1;
		if(this.lastModified != mediaFile.getLastModified()) return -1;
		
		return 0;
	}

	public abstract Type getType();
	
	
	public String toString() {
		return getType() + 
				"\nPath:"+path+
				"\nSize:"+size+
				"\nName:"+name+
				"\nLastModified:"+lastModified;
	}
}
 